using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DBSourceToolsLib.UI.Forms
{
    public partial class StoredProcOptionsForm : Form
    {
        private string m_strDatabaseName;
//        private string m_strDatabasesXmlFile;
        private string m_strStoredProcOptionsXmlFile;
        private bool m_bHasReloaded = false;

        public StoredProcOptionsForm(string strDatabaseName, string strScriptOptionsXmlFile)
        {
            InitializeComponent();

            this.m_strDatabaseName = strDatabaseName;
//            this.m_strDatabasesXmlFile = strDatabasesXmlFile;
            this.m_strStoredProcOptionsXmlFile = strScriptOptionsXmlFile;

            if (System.IO.File.Exists(this.m_strStoredProcOptionsXmlFile))
            {
                this.m_scriptOptionsDS.ReadXml(this.m_strStoredProcOptionsXmlFile);
                this.m_scriptOptionsDS.ScriptOptions.AcceptChanges();
            }
            this.m_ListView.DataSet = this.m_scriptOptionsDS;
            this.m_ListView.AddColumn("TableName", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Text);
            this.m_ListView.AddColumn("ScriptTable", DBSourceToolsLib.SysObjects.UI.DBSourceListViewColType.Checkbox);
            this.m_ListView.Bind();

        }


        private void btnReload_Click(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FileUtils.EnsureFileIsWriteable(this.m_strStoredProcOptionsXmlFile);
            this.m_scriptOptionsDS.WriteXml(this.m_strStoredProcOptionsXmlFile);
        }

        private void m_toolStripButtonReload_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            SourceDatabase oSourceDatabase
                = DBSourceToolsLib.UserSettings.Instance().getSourceDatabase(m_strDatabaseName);

            DBSourceToolsLib.ScriptOptionsLoader oLoader = new ScriptOptionsLoader(
                this, DBSourceToolsLib.Commands.DatabaseCommandTypes.LoadStoredProcList);


            oLoader.Execute(ref oSourceDatabase, null);

            this.m_scriptOptionsDS = oLoader.ProcScriptOptionsDS;
            this.m_ListView.DataSet = this.m_scriptOptionsDS;
            this.m_ListView.Bind();
            this.Cursor = Cursors.Default;

        }

        private void m_toolStripButtonSave_Click(object sender, EventArgs e)
        {
            this.m_scriptOptionsDS.AcceptChanges();
            FileUtils.EnsureFileIsWriteable(m_strStoredProcOptionsXmlFile);
            this.m_scriptOptionsDS.WriteXml(this.m_strStoredProcOptionsXmlFile);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = true;
            }
            this.m_ListView.Invalidate();

        }

        private void selectNoneToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
            {
                oRow["ScriptTable"] = false;
            }
            this.m_ListView.Invalidate();

        }

        private void StoredProcOptionsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.m_scriptOptionsDS.HasChanges())
            {
                if (MessageBox.Show(this, "Save Changes ?", "Table Options", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    FileUtils.EnsureFileIsWriteable(this.m_strStoredProcOptionsXmlFile);
                    this.m_scriptOptionsDS.WriteXml(this.m_strStoredProcOptionsXmlFile);
                }
            }

        }

        private void selectUsingFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SysObjects.UI.Forms.SelectFilterForm oSelectFilterForm = new DBSourceToolsLib.SysObjects.UI.Forms.SelectFilterForm();
            if (oSelectFilterForm.ShowDialog(this) == DialogResult.OK)
            {
                string strFilterString = oSelectFilterForm.SelectionFilter;
                if (!String.IsNullOrEmpty(strFilterString))
                {
                    foreach (DataRow oRow in this.m_scriptOptionsDS.Tables[0].Rows)
                    {
                        if (oRow["TableName"].ToString().IndexOf(strFilterString) == 0)
                        {
                            oRow["ScriptTable"] = true;
                        }
                    }
                }
            }
            this.m_ListView.Invalidate();
        }
    }
}